package com.zha.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zha.boss.constants.SysConstants;
import com.zha.boss.dao.entity.UserPayConfig;
import com.zha.boss.dao.entity.UserPayInfo;
import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.mvc.convert.UserPayConfigConvert;
import com.zha.boss.mvc.models.forms.config.UserPayConfigForm;
import com.zha.boss.mvc.models.resp.AjaxResult;
import com.zha.boss.mvc.models.resp.PageInfo;
import com.zha.boss.mvc.models.vo.UserPayConfigVo;
import com.zha.boss.mvc.service.UserPayConfigService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zha
 * @CreateTime: 2024年09月17日 10:54
 */
@RestController
public class UserPayConfigController {
    @Resource
    private UserPayConfigConvert userPayConfigConvert;
    @Resource
    private UserPayConfigService userPayConfigService;

    /**
     * 列表支付方式
     */
    @GetMapping(value = "/pay/config/pageList")
    public AjaxResult pageList(Integer pageNo) {
        pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
        IPage<UserPayConfig> userPayConfigIPage = userPayConfigService.pageList(pageNo, SysConstants.PAGE_SIZE);
        // page 分页
        PageInfo pageInfo = new PageInfo(userPayConfigIPage.getCurrent(),userPayConfigIPage.getPages());
        // 转vo
        List<UserPayConfigVo> listUserPayConfigVo = userPayConfigConvert.toListUserPayConfigVo(userPayConfigIPage.getRecords());
        Map<String, Object> data = new HashMap<>();
        data.put("page", pageInfo);
        data.put("list", listUserPayConfigVo);
        return AjaxResult.ok(data);
    }
    /**
     * 添加用户的支付配置
     */
    @PostMapping(value = "/pay/config/addConfig")
    private AjaxResult addUserPayConfig(@RequestBody UserPayConfigForm userPayConfigForm) {
        userPayConfigService.addUserPayConfig(userPayConfigForm);
        return AjaxResult.ok();
    }
    /**
     * 查询第三方支付配置信息
     */
    @GetMapping(value = "/pay/config/infoList")
    public AjaxResult payInfoList(String userNo,String productCode) {
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)){
            List<UserPayInfo> userPayInfoList = userPayConfigService.payInfoList(userNo,productCode);
            return AjaxResult.ok(userPayConfigConvert.toListUserPayInfoVo(userPayInfoList));
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }
    /**
     * 删除用户的支付配置
     */
    @PostMapping(value = "/pay/config/remove")
    private AjaxResult removeUserPayConfig(String userNo,String productCode) {
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)){
            userPayConfigService.removeUserPayConfig(userNo,productCode);
            return AjaxResult.ok();
        }
        return AjaxResult.warn(RespCode.REQUEST_PARAM_ERR);
    }
}

